package com.xwj.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.xwj.Enums.StatusInfoEnum;
import com.xwj.data.StaticData;
import com.xwj.dto.CloudHouseInfoDto;
import com.xwj.dto.CreateCloudHouseInfoDto;
import com.xwj.entity.RaCloudHourse;
import com.xwj.entity.ResponseInfoEntity;
import com.xwj.service.Impl.RaCloudHourseServiceImpl;
import com.xwj.service.Impl.RaStatusInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuweijie on 2018/1/18.
 */
@RestController
@RequestMapping("/admin/house/")
public class HouseController {

    @Autowired
    private RaCloudHourseServiceImpl raCloudHourseService;
    @Autowired
    private RaStatusInfoServiceImpl raStatusInfoService;

    @RequestMapping("houseList")
    public ModelAndView houselist(ModelAndView modelAndView) {
        String label = StatusInfoEnum.CloudHouse.getName();
        modelAndView.addObject("cloudStatus", raStatusInfoService.findStatusByLabel(label));
        modelAndView.setViewName("/admin/house/house");
        return modelAndView;
    }

    @RequestMapping(value = "cloudHouseInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, Object> cloudHouseInfo(@RequestParam(value = "page", required = false) Integer pageIndex,
                                              @RequestParam(value = "limit", required = false) Integer pageSize,
                                              @RequestParam(value = "key", required = false) String key) {
        PageInfo<RaCloudHourse> page;
        if (pageIndex == null) pageIndex = 1;
        if (pageSize == null) pageSize = StaticData.PAGE_SIZE;
        CloudHouseInfoDto cloudHouseInfoDto = null;
        if (key != null) cloudHouseInfoDto = JSON.parseObject(key, new TypeReference<CloudHouseInfoDto>() {
        });
        if (cloudHouseInfoDto != null)
            page = raCloudHourseService.findALLByCondition(pageIndex, pageSize, cloudHouseInfoDto);
        else
            page = raCloudHourseService.findAllCloudHouse(pageIndex, pageSize, "cloudHourse_Page_" + pageIndex);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", page.getTotal());
        map.put("data", page.getList());
        return map;
    }

    /**
     * 删除(单条)
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "deleteCloudData", method = RequestMethod.POST)
    public synchronized Map<String, Object> deleteCloudData(@RequestParam(value = "myData") String data) throws IOException, URISyntaxException {
        Map<String, Object> map = new HashMap<>();
        RaCloudHourse raCloudHourse = JSON.parseObject(data, new TypeReference<RaCloudHourse>() {
        });
        ResponseInfoEntity responseInfoEntity = raCloudHourseService.deleteRequest(raCloudHourse.getCloudId());
        if (responseInfoEntity.getInfo().equalsIgnoreCase("ok")) {
            if (raCloudHourseService.deleteOneById(raCloudHourse.getCloudId()) > 0)
                map.put("msg", "删除成功");
            else
                map.put("msg", "已从地图上删除,但数据异常！");
        } else
            map.put("msg", "删除失败");
        return map;
    }

    /**
     * 批量删除
     *
     * @param data
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @RequestMapping(value = "deleteCloudDataBatch", method = RequestMethod.POST)
    public synchronized Map<String, Object> deleteCloudDataBatch(@RequestParam(value = "myData") String data) throws IOException, URISyntaxException {
        Map<String, Object> map = new HashMap<>();
        List<RaCloudHourse> raCloudHourseList = JSON.parseArray(data, RaCloudHourse.class);
        if (raCloudHourseList.size() == 0) {
            map.put("msg", "未选中任何选项");
            return map;
        }
        List<Integer> cloudIdList = raCloudHourseService.parseListInt(raCloudHourseList);
        String str = raCloudHourseService.parseString(cloudIdList);
        if (StringUtils.isBlank(str)) {
            map.put("msg", "参数有误");
            return map;
        }
        ResponseInfoEntity responseInfoEntity = raCloudHourseService.deleteRequest(str);
        if (responseInfoEntity.getInfo().equalsIgnoreCase("ok")) {
            if (raCloudHourseService.deletebatchById(cloudIdList) > 0)
                map.put("msg", "删除成功");
            else
                map.put("msg", "已从地图上删除,但数据异常！");
        } else
            map.put("msg", "删除失败");
        return map;
    }

    /**
     * 单次创建
     *
     * @param data
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @RequestMapping(value = "createOneCloudData", method = RequestMethod.POST)
    public synchronized Map<String, Object> createOneCloudData(@RequestParam(value = "myData") String data) throws IOException, URISyntaxException {
        Map<String, Object> map = new HashMap<>();
        CreateCloudHouseInfoDto cloudHouseInfoDto = JSON.parseObject(data, new TypeReference<CreateCloudHouseInfoDto>() {
        });
        cloudHouseInfoDto.setId(StringUtils.EMPTY);
        if (StringUtils.isNotBlank(raCloudHourseService.findOneByLocation(cloudHouseInfoDto.getLon(), cloudHouseInfoDto.getLat()))) {
            map.put("msg", "地点已经存在，请勿重复添加");
            return map;
        }
        ResponseInfoEntity responseInfoEntity = raCloudHourseService.createOrUpdateOneReuest(cloudHouseInfoDto);
        if (responseInfoEntity.getInfo().equalsIgnoreCase("ok")) {
            RaCloudHourse raCloudHourse = new RaCloudHourse(responseInfoEntity.getId(), cloudHouseInfoDto.getName(),
                    cloudHouseInfoDto.getAddress(), cloudHouseInfoDto.getLon(), cloudHouseInfoDto.getLat());
            if (raCloudHourseService.insertOne(raCloudHourse) > 0)
                map.put("msg", "添加成功");
            else
                map.put("msg", "上传成功，保存失败");
        } else
            map.put("msg", "上传数据失败");
        return map;
    }

    /**
     * 更新云图数据
     *
     * @param id
     * @param data
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @RequestMapping(value = "updateOneCloudData", method = RequestMethod.POST)
    public synchronized Map<String, Object> updateOneCloudData(@RequestParam(value = "id") int id, @RequestParam(value = "myData") String data) throws IOException, URISyntaxException {
        Map<String, Object> map = new HashMap<>();
        CreateCloudHouseInfoDto cloudHouseInfoDto = JSON.parseObject(data, new TypeReference<CreateCloudHouseInfoDto>() {
        });
        cloudHouseInfoDto.setId(String.valueOf(id));
        if (StringUtils.isNotBlank(raCloudHourseService.findOneByLocation(cloudHouseInfoDto.getLon(), cloudHouseInfoDto.getLat()))) {
            map.put("msg", "地点已经存在，请重新修改");
            return map;
        }
        ResponseInfoEntity responseInfoEntity = raCloudHourseService.createOrUpdateOneReuest(cloudHouseInfoDto);
        if (responseInfoEntity.getInfo().equalsIgnoreCase("ok")) {
            if (raCloudHourseService.updateOneByParam(cloudHouseInfoDto) > 0)
                map.put("msg", "更新成功");
            else
                map.put("msg", "云图更新成功，数据库更新失败");
        } else
            map.put("msg", "更新云图数据失败");
        return map;
    }

}
